package com.unusap.remote.core.codec;

import com.unusap.remote.core.codec.exception.DecryptionException;
import com.unusap.remote.core.codec.exception.EncryptException;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: danson
 * Date: 2009-8-21
 * Time: 11:13:53
 * To change this template use File | Settings | File Templates.
 */
public class Des3Encoder {

    public static final Des3Encoder INSTANCE = new Des3Encoder();

    private static final String Algorithm = "DESede";
    private static final String ADD_STRING = "000000000000000000000000";
    private static final int KEY_LENGTH = 24;
    public static final String KEY = "999999999999999";

    private Des3Encoder() {
    }

    public String encrypt(String string, String key) {
        try {
            //加密
            Cipher cipher = Cipher.getInstance(Algorithm);
            // 密钥字节数组
            byte[] keybytes = getKeyBytes(key);
            // 密钥
            SecretKey deskey = getKey(keybytes);
            cipher.init(Cipher.ENCRYPT_MODE, deskey);
            // 密文数组
            byte[] encbytes = cipher.doFinal(string.getBytes());
            // 加密后的密文
            return base64Encoder(encbytes);
        } catch (Exception e) {
            throw new EncryptException(e);
        }
    }

    public String decrypt(String string, String key) {
        try {
            // 加密
            Cipher cipher = Cipher.getInstance(Algorithm);
            // 密钥字节数组
            byte[] keybytes = getKeyBytes(key);
            // 密钥
            SecretKey deskey = getKey(keybytes);
            cipher.init(Cipher.DECRYPT_MODE, deskey);
            // 解密后的字符串
            return new String(cipher.doFinal(base64Decoder(string)));
        } catch (Exception e) {
            throw new DecryptionException(e);
        }
    }

    // 检查密钥长充,如不足24位,则加零补齐
    public byte[] getKeyBytes(String keyString) {
        int count = KEY_LENGTH - keyString.length();
        keyString += ADD_STRING.subSequence(0, count);
        return keyString.getBytes();
    }

    // 获取密钥
    public SecretKey getKey(byte[] keyBytes) {
        return new SecretKeySpec(keyBytes, Algorithm);
    }

    // base64转码
    public String base64Encoder(byte[] b) {
        return new BASE64Encoder().encode(b);
    }

    // base64解码
    public byte[] base64Decoder(String str) {
        try {
            return new BASE64Decoder().decodeBuffer(str);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
		// 密钥
		String key = "dfs";
		// 原文
		String src = "开玩笑";
		System.out.println("原 文:" + src);
		Des3Encoder tool = Des3Encoder.INSTANCE;
//		 tool.test(src, key);
		// 密文
		String desString = "";
		desString = tool.encrypt(src, key);
		System.out.println("加密结果:" + desString);
		String newSrc = tool.decrypt(desString, key);
		System.out.println("解密结果:" + newSrc);
	}

}
